<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <table border="">
        <thead>
            <tr>
                <th>姓名</th>
                <th>年龄</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>张三</td>
                <td>20</td>
            </tr>
        </tbody>
    </table>
    <script>

        var arr = [{
            name: "张三",
            age: 20
        }, {
            name: "李四",
            age: 30
        },
        {
            name: "王五",
            age: 20
        }, {
            name: "王小二333",
            age: 20
        },{
            name: "王小二333",
            age: 20
        },{
            name: "王小二333",
            age: 20
        },{
            name: "王小二333",
            age: 20
        }]
        var htmlstr = "";
        for (var i = 0; i < arr.length; i++) {
            if (i % 2 == 0) {
                // 偶数行   
                htmlstr += '<tr style="background:red"><td>' + arr[i].name + '</td><td>' + arr[i].age + '</td></tr>';

            } else {
                // 奇数行
                htmlstr += '<tr><td>' + arr[i].name + '</td><td>' + arr[i].age + '</td></tr>';

            }
            // console.log(arr[i]);
        }
        console.log(htmlstr);
        var tbodyEle = document.querySelector("tbody");
        tbodyEle.innerHTML = htmlstr;

    </script>
</body>

</html>